package com.hcb.micro.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hcb.micro.controller.base.BaseControllers;
import com.hcb.micro.dto.Mall;
import com.hcb.micro.dto.Manager;
import com.hcb.micro.service.IMallService;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("mall")
public class MallController extends BaseControllers{
	
	@Autowired
	IMallService mallService;
	
	@RequestMapping(value = "list", method = RequestMethod.POST)
	@ResponseBody
	public String list() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		Map<String,Object> para = new HashMap<>();
		
		if (bodyInfo.get("mallId") != null && !"".equals(bodyInfo.get("mallId"))) {
			para.put("mallId", bodyInfo.getString("mallId"));
		}
		if(bodyInfo.get("mallName") != null && !"".equals(bodyInfo.get("mallName"))) {
			para.put("mallName", bodyInfo.getString("mallName"));
		}
		
		Mall mall = mallService.selectByMallUuid(manager.getMallUuid()); 
		if(mall != null && !"商盟惠购".equals(mall.getMallName())){
			para.put("mallUuid", mall.getMallUuid());
		}
		
		List<Mall> mallListAll = mallService.selectByPara(para);
		
		if(bodyInfo.get("page") != null && !"".equals(bodyInfo.get("page"))) {
			para.put("start", (Integer.valueOf(bodyInfo.getString("page"))-1)*Integer.valueOf(bodyInfo.getString("pageSize")));
		}
		if(bodyInfo.get("pageSize") != null && !"".equals(bodyInfo.get("pageSize"))) {
			para.put("size", Integer.valueOf(bodyInfo.getString("pageSize")));
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		List<Mall> mallList = mallService.selectByPara(para);
		
		if(mallList.size() <= 0){
			json.put("result", "1");
			json.put("description", "商城列表不存在");
			return buildReqJsonObject(json);
		}
		List<Map<String,String>> list = new ArrayList<>();
		for(Mall m:mallList){
			Map<String,String> map = new HashMap<>();
			
			map.put("id", m.getId()+"");
			map.put("mallUuid", m.getMallUuid());
			map.put("mallName", m.getMallName());
			map.put("mallLogo", m.getMallLogo()==null?"":m.getMallLogo());
			map.put("mallColor", m.getMallColor()==null?"":m.getMallColor());
			map.put("companyName", m.getCompanyName()==null?"":m.getCompanyName());
			map.put("contactPhone", m.getContactPhone()==null?"":m.getContactPhone());
			map.put("createTime", m.getCreateTime()==null?"":sdf.format(m.getCreateTime()));
			list.add(map);
		}
		
    	json.put("result", "0");
		json.put("description", "获取商城列表成功");
		json.put("list", list);
		json.put("count", mallListAll.size());
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "detail", method = RequestMethod.POST)
	@ResponseBody
	public String detail() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		int right = 1;
		if(bodyInfo.get("mallId") == null || "".equals(bodyInfo.get("mallId"))) {
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		Mall mall = mallService.selectByPrimaryKey(Integer.valueOf(bodyInfo.getString("mallId")));
		
		if(mall == null){
			json.put("result", "1");
			json.put("description", "商城不存在");
			return buildReqJsonObject(json);
		}
		
		Map<String,String> map = new HashMap<>();
		map.put("mallId", mall.getId()+"");
		map.put("mallName", mall.getMallName());
		map.put("mallLogo", mall.getMallLogo()==null?"":mall.getMallLogo());
		map.put("mallColor", mall.getMallColor()==null?"":mall.getMallColor());
		map.put("companyName", mall.getCompanyName()==null?"":mall.getCompanyName());
		map.put("contactPhone", mall.getContactPhone()==null?"":mall.getContactPhone());
		map.put("createTime", mall.getCreateTime()==null?"":sdf.format(mall.getCreateTime()));
		
    	json.put("result", "0");
		json.put("description", "获取商城成功");
		json.put("mall", map);
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "modify", method = RequestMethod.POST)
	@ResponseBody
	public String modify() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		int right = 1;
		if(bodyInfo.get("mallId") == null || "".equals(bodyInfo.get("mallId"))) {
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		Mall mall = mallService.selectByPrimaryKey(Integer.valueOf(bodyInfo.getString("mallId")));
		
		if(mall == null){
			json.put("result", "1");
			json.put("description", "商城不存在");
			return buildReqJsonObject(json);
		}
		
		if(bodyInfo.get("mallName") != null && !"".equals(bodyInfo.get("mallName"))) {
			mall.setMallName(bodyInfo.getString("mallName"));
		}
		if(bodyInfo.get("mallLogo") != null && !"".equals(bodyInfo.get("mallLogo"))) {
			mall.setMallLogo(bodyInfo.getString("mallLogo"));
		}
		if(bodyInfo.get("mallColor") != null && !"".equals(bodyInfo.get("mallColor"))) {
			mall.setMallColor(bodyInfo.getString("mallColor"));
		}
		if(bodyInfo.get("companyName") != null && !"".equals(bodyInfo.get("companyName"))) {
			mall.setCompanyName(bodyInfo.getString("companyName"));
		}
		if(bodyInfo.get("contactPhone") != null && !"".equals(bodyInfo.get("contactPhone"))) {
			mall.setContactPhone(bodyInfo.getString("contactPhone"));
		}
		
		Integer rs = mallService.updateByPrimaryKeySelective(mall);
		if(rs != 1){
			json.put("result", "1");
			json.put("description", "修改商城失败");
		}else{
			json.put("result", "0");
			json.put("description", "修改商城成功");
		}
		return buildReqJsonObject(json);
	}
}
